<?php

/**
 * This is the model class for table "mangas".
 *
 * The followings are the available columns in table 'mangas':
 * @property integer $id
 * @property string $title
 * @property string $author
 * @property string $artist
 * @property string $tags
 * @property string(4) $extension
 * @property string $createdAt
 * @property string $updatedAt
 */
class Manga extends ActiveRecord {

	public $cover;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Manga the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'mangas';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array(
			array('title', 'required'),
			array('title, author, artist, tags', 'length', 'max'=>255),
			array(
				'cover',
				'EPhotoValidator',
				'allowEmpty'=>true,
				'mimeType'=>array('image/jpeg', 'image/png'),
			),
			array('title, author, artist, tags', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'chapters'=>array(self::HAS_MANY, 'Chapter', 'mangaId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'title'=>'4-Koma',
			'author'=>'Author',
			'artist'=>'Artist',
			'tags'=>'Tags',
			'cover'=>'Cover',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;
		$criteria->order = 'updatedAt DESC';
		$criteria->compare('title', $this->title, true);
		$criteria->compare('author', $this->author, true);
		$criteria->compare('artist', $this->artist, true);
		$criteria->compare('tags', $this->tags, true);

		return new CActiveDataProvider($this, array('criteria'=>$criteria));
	}

	protected function beforeSave() {
		$this->cover = CUploadedFile::getInstance($this, 'cover');
		if ($this->cover) {
			$this->extension = $this->cover->extensionName;
		}
		return parent::beforeSave();
	}

	protected function afterSave() {
		parent::afterSave();
		if ($this->cover) {
			$this->cover->saveAs($this->coverPath);
			$image = Yii::app()->image->load($this->coverPath);
			$image->resize(150, NULL, 4)->quality(75);
			$image->save($this->coverThumbnailPath);
		}
	}

	public function getCoverPath() {
		$folder = Yii::app()->params['files'] . '/mangas';
		return "{$folder}/{$this->id}.{$this->extension}";
	}

	public function getCoverThumbnailPath() {
		$folder = Yii::app()->params['files'] . '/mangas/_thumbnails';
		return "{$folder}/{$this->id}.jpg";
	}

	public function getCoverUrl() {
		$baseUrl = Yii::app()->request->baseUrl;
		return "$baseUrl/files/mangas/{$this->id}.{$this->extension}";
	}

	public function getCoverThumbnailUrl() {
		$baseUrl = Yii::app()->request->baseUrl;
		return "$baseUrl/files/mangas/_thumbnails/{$this->id}.jpg";
	}
	
	public function listData() {
		$criteria = new CDbCriteria();
		$criteria->order = 'title';
		$criteria->compare('title', $this->title, true);
		$criteria->compare('author', $this->author, true);
		$criteria->compare('artist', $this->artist, true);
		$criteria->compare('tags', $this->tags, true);
		$models = self::model()->findAll($criteria);
		return CHtml::listData($models, 'id', 'title');
	}

}